// pages/review/review.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';

const app = getApp()
var correctNum = 0;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataOK: false,
    num: 0,   //题目总数
    typeInfo: [
      {
        label: '未分类',
        eg: '暂无'
      },
      {
        label: '整数加减法',
        eg: '16 + 28 = 44'
      },
      {
        label: '整数乘除法',
        eg: '22 × 2 = 11'
      },
      {
        label: '小数加减法',
        eg: '1.6 + 0.4 = 2.0'
      },
      {
        label: '小数乘除法',
        eg: '1.2 ÷ 2.0 = 0.6'
      },
      {
        label: '小数比大小',
        eg: '1.56 < 1.7'
      },
      {
        label: '分数加减法',
        eg: '1/2 - 1/3 = 1/6'
      },
      {
        label: '分数乘除法',
        eg: '1/3 ÷ 1/2 = 2/3'
      },
      {
        label: '分数比大小',
        eg: '1/3 < 1/2'
      },
      {
        label: '四则运算',
        eg: '1+2*3=7'
      },
    ],
    r: ['⚪', '<', '>', '='],
    check: [], //是否查看答案
    collect: [],  //题目是否被收藏
    problems: []  //问题数组（由ks-api得来）
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    //请求题目
    this.getCollections();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   * 要将所有收藏上传服务端
   */
  onUnload: function () {
    var collections = {
      env: 'review',
      openid: app.globalData.openid,
      ord4cancel: [],
      num: 0,
      details: []
    }
    for (let i = 0; i < this.data.num; i++) {
      if (!this.data.collect[i]) {
        collections.ord4cancel.push(i)  //要取消收藏的编号
      }
    }
    if (collections.ord4cancel.length == 0) {return;}
    //调用ks-api更新收藏
    wx.cloud.callContainer({
      config: {
        env: "prod-6gmuxgtca5913e7c"
      },
      path: "/collections",
      header: {
        'X-WX-SERVICE': "ks-api",
        'content-type': "application/json"
      },
      method: "POST",
      data: collections,
      success: function (res) {
        if (res.statusCode != 200) {
          Notify({
            type: 'danger',
            message: '收藏更新失败。'
          })
        } else {
          Notify({
            type: 'success',
            message: '收藏更新成功！'
          })
        }
      },
      failed: function (res) {
        Notify({
          type: 'danger',
          message: '收藏更新失败。'
        })
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 获取收藏
   */
  getCollections() {
    var that = this
    //调用ks-api获取收藏
    wx.cloud.callContainer({
      config: {
        env: "prod-6gmuxgtca5913e7c"
      },
      path: "/problems",
      header: {
        'X-WX-SERVICE': "ks-api",
        'content-type': "application/json"
      },
      method: "GET",
      data: {
        env: 'review',
        grade: 0,
        num: 0,
        openid: app.globalData.openid
      },
      success: function (res) {
        if (res.statusCode != 200) {
          Dialog.confirm({
            title: '加载失败',
            message: '题目不翼而飞了呢，请检查网络连接。',
          })
            .then(() => {
              // on confirm
              wx.navigateBack({
                delta: 1,
              })
            })
        } else {
          that.setData({
            num: res.data.num,
            problems: res.data.details
          })
          let _collect = [];
          let _check = [];
          //初始化
          for (let i = 0; i < that.data.num; i++) {
            _collect.push(true);
            _check.push(false);
          }
          that.setData({
            collect: _collect,
            check: _check,
            dataOK: true,
          })
        }
      },
      failed: function (res) {
        Dialog.confirm({
          title: '加载失败',
          message: '题目不翼而飞了呢，请检查网络连接。',
        })
        .then(() => {
          // on confirm
          wx.navigateBack({
            delta: 1,
          })
        })
      }
    })
  },

  /**
   * 用户收藏题目
   */
  onCollect(event) {
    let id = event.currentTarget.dataset.id;
    let isCollected = this.data.collect[id]
    if (isCollected) {
      Notify({
        background: '#FFFFFF',
        color: '#000000',
        message: '取消收藏 ×',
        duration: 1000
      })
    } else {
      Notify({
        background: '#fffe02',
        color: '#000000',
        message: '收藏成功 √',
        duration: 1000
      })
    }
    this.setData({
      [`collect[${id}]`]: !isCollected
    })
  },

  /**
   * 用户查看答案
   */
  onCheck(event) {
    let id = event.currentTarget.dataset.id;
    this.setData({
      [`check[${id}]`]: !this.data.check[id]
    })
  }
})